<?php
	/**
	 * 订单控制器
	 */
	class OrderAction extends Action{

		//确认订单视图
		public function confirmOrder(){
			$data = $_SESSION['cart'];
			$sumPrice = 0;
			foreach($data as $key=>$val){
				// $data[$key]['number'] = $_POST['buynumberh'][$key];
				// $data[$key]['totalPrice'] = number_format((int)$data[$key]['sprice']*(int)$_POST['buynumberh'][$key] , '2');
				// $sumPrice +=(int)$data[$key]['sprice']*(int)$_POST['buynumberh'][$key];
				$sumPrice +=(int)$data[$key]['sprice']*(int)$data[$key]['number'];
			}
			$this->sumPrice = $sumPrice;
			$this->data=$data;
			if(isset($_SESSION['userid'])){
				$userid = $_SESSION['userid'];
				$db = M("user_address");
				$result = $db->where(array("uid"=>$_SESSION['userid']))->select();
				if(count($result)>0){
					$this->address=$result;
				}
				// p($result);
				// p($_SESSION);
			}
			$cityDb = M('city');
			$cityList = $cityDb->field('cid,city')->where("pid=0")->select();
			$this->cityList = $cityList;
			$this->display();	
		}

		public function index(){
			$this->display();
		}

		//处理提交的订单数据
		public function addOrder(){
			if($this->isPost()){

				$tmpData = $_SESSION['cart'];
				// p($tmpData);
				// p($_POST);
				$consigneeInfo = M('user_address')->where('address_id='.$this->_post('address'))->find();
				// die;
				$sumPrice = 0.00;
				foreach($tmpData as $key=>$val){
					$tmpData[$key]['number'] = $_POST['buynumberh'][$key];
					$tmpData[$key]['totalPrice'] = number_format((int)$tmpData[$key]['sprice']*(int)$_POST['buynumberh'][$key] , '2');
					$sumPrice +=(int)$tmpData[$key]['sprice']*(int)$_POST['buynumberh'][$key];
				}
				//计算运费
				if($this->_post('pstype') == '1'){
					$transportPrice = 15;
				}else if($this->_post('pstype') == '2'){
					$transportPrice = 20;
				}
				$sumPrice = $sumPrice + $transportPrice;
				$usermsg = $this->_post('usermsg');
				if($usermsg =='选填，可以告诉卖家您对商品的特殊要求'){
					$usermsg='';
				}
				foreach($_SESSION['cart'] as $val){
					$order_list[]= array(
						'gid' => $val['gid'],
						'quantity' => $val['number'],
						'subtotal' => $val['totalPrice'],
						'mark' => $val['color'] . '&nbsp' . $val['size'],
						);	 
				}
				$data = array(
					'number' => "hs".time().mt_rand(0,999),
					'consignee' => $consigneeInfo['consignee'],
					'address' => $consigneeInfo['location'],
					'total'=> $sumPrice,
					'status' => 1,
					'remark' => $usermsg,
					'uid' => $_SESSION['userid'],
					'time' => time(),
					'transporttype' =>$this->_post('pstype'),
					'paytype' =>$this->_post('paytype'),
					'order_list' => $order_list
					);
				$db = D('OrderRelation');
				// p($data);die;
				$oid = $db->relation(true)->add($data);
				// echo $db->getLastSql();die;
				if($oid){
					$_SESSION['cart'] = NULL;
					$_SESSION['cartGsNum'] = 0;
					$_SESSION['oid'] = $oid;
					$this->success('订单提交成功!', U('showOrder',array('id' => $oid)));
				}else{
					$this->error('订单提交失败,请刷新页面后重试！');
				}
			}
		}

		//订单提交成功后显示订单信息
		public function showOrder(){
			if($this->_get('id')){
				$oid = $this->_get('id','intval');
				$db = D('OrderRelation');
				$orderData = $db->relation(true)->field('number,total,paytype,transporttype,status')->where(array('id'=>$oid))->find();
				$this->orderData = $orderData;
				$this->display();
			}
		}
		/**
		 * 付款成功确认收货页面
		 * @return [type] [description]
		 */
		public function toShouHuo(){
			if($this->isAjax()){
				$oid = $this->_post('oid');
				if(M('order')->where('id='.$oid)->save(array('status'=>2))){
					echo json_encode(array('status'=>'ok'));
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}else{
				$db = D('OrderRelation');
				$oid = $_SESSION['oid'];
				$orderData = $db->relation(true)->field('number,total,paytype,transporttype,status')->where(array('id'=>$oid))->find();
				$this->orderData = $orderData;
				// p($orderData);
				$this->display();
			}
		}

		/**
		 * 确认收货处理
		 * @return [type] [description]
		 */
		public function confrimSH(){
			if($this->isAjax()){
				$oid = $this->_post('oid');
				if(M('order')->where('id='.$oid)->save(array('status'=>4))){
					echo json_encode(array('status'=>'ok'));
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}	
		}

		/**
		 * 已确认收货，购物完成
		 * @return [type] [description]
		 */
		public function shopOk(){
			$this->display();
		}

		/**
		 * 获取城市信息
		 * @return [type] [description]
		 */
		public function getCity(){
			if($this->isAjax()){
				$pid = $this->_post('cid');
				$db = M("city");
				$result = $db->where("pid=".$pid)->select();
				if(is_array($result) && count($result)>0){
					$result['status'] = 'ok';
					echo json_encode($result);	

				}else{
					echo json_encode(array('status'=>'er'));
				}
				
			}
		}

		/**
		 * 增加新的收货地址
		 */
		public function addNewAddress(){
			if($this->isAjax()){
				$cityids = $this->_post('address');
				$cityids = rtrim($cityids,'|');
				$cityid = rtrim($cityids ,'|');
				$cityid = explode('|', $cityids);
				$area = '';
				foreach($cityid as $val){
					$result = M('city')->field('city')->where('cid='.$val)->find();
					$area.=$result['city'];
				}
				$consignee = $this->_post('consignee');
				$addressDetail = $this->_post('addressD');
				$email = $this->_post('email');
				$tel = $this->_post('tel');
				$data = array(
					'uid'=>$_SESSION['userid'],
					'consignee'=>$consignee,
					'moblie'=>$tel,
					'email'=>$email,
					'location'=>$area.','.$addressDetail,
					'cityid'=>$cityids
					);
				$db = M('user_address');
				if($db->add($data)){
					echo json_encode(array('status'=>'ok','location'=>$area.','.$addressDetail));
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}
		}

		/**
		 * 删除收货地址
		 * @return [type] [description]
		 */
		public function delAddress(){
			if($this->isAjax()){
				$aid = $this->_post('aid');
				$db = M('user_address');
				if($db->where("address_id=".$aid)->delete()){
					echo json_encode(array('status'=>'ok'));
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}
		}

		/**
		 * 设置默认收货地址
		 */
		public function setDefAddress(){
			if($this->isAjax()){
				$aid = $this->_post('aid');
				$db = M('user_address');
				$db->where('uid='.$_SESSION['userid'])->save(array("add_default"=>0));
				$result = $db->where('address_id='.$aid)->save(array("add_default"=>1));
				if($result){
					echo json_encode(array('status'=>'ok'));
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}
		}

		/**
		 * 获取修改收货地址需要的数据
		 * @return [type] [description]
		 */
		public function modifyAddress(){
			if($this->isAjax()){
				$aid = $this->_post('aid');
				$result = M('user_address')->where('address_id='.$aid)->find();
				if(is_array($result)){
					$result['status']='ok';
					$tmp = explode(',', $result['location']);
					$result['locationinfo'] = $tmp[1];
					unset($result['location']);
					$cityList = explode('|', $result['cityid']);
					$tmpArr=array();
					$db = M('city');
					foreach($cityList as $key=>$val){
						$tmpVal = $db->where('pid='.$val)->select();
						if(is_array($tmpVal)){
							$tmpArr[$key] = $tmpVal;
						}
					}
					$result['citys'] = $tmpArr;
					echo json_encode($result);
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}
		}

		/**
		 * 修改收货地址
		 * @return [type] [description]
		 */
		public function updateAddress(){
			if($this->isAjax()){
				$aid = $this->_post('aid');
				$cityids = $this->_post('address');
				$cityids = rtrim($cityids,'|');
				$cityid = rtrim($cityids ,'|');
				$cityid = explode('|', $cityids);
				$area = '';
				foreach($cityid as $val){
					$result = M('city')->field('city')->where('cid='.$val)->find();
					$area.=$result['city'];
				}
				$consignee = $this->_post('consignee');
				$addressDetail = $this->_post('addressD');
				$email = $this->_post('email');
				$tel = $this->_post('tel');
				$data = array(
					'consignee'=>$consignee,
					'moblie'=>$tel,
					'email'=>$email,
					'location'=>$area.','.$addressDetail,
					'cityid'=>$cityids
					);
				$db = M('user_address');

				if($db->where('address_id='.$aid)->save($data)){
					echo json_encode(array('status'=>'ok'));
				}else{
					echo json_encode(array('status'=>'er'));
				}
			}
		}

	}
?>